package com.lin.databases;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Properties;

/**
 * @author: zhengwenzhou
 * @date: 2017/10/12
 * @description:
 */
@Slf4j
@Configuration
@MapperScan({"com.lin.sys.dao.**"})
@EnableTransactionManagement
public class DatabaseServer {


    @Bean(name = "service_0")
    @Primary
    public DataSource service0() throws Exception {
        DruidDataSource datasource = new DruidDataSource();
        Properties properties = new Properties();
        try {
            properties.load(this.getClass().getClassLoader().getResourceAsStream("service0.properties"));
            datasource.setConnectProperties(properties);
            log.info("数据库 :" + datasource.getUrl() + " 初始化成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return datasource;
    }

    @Bean(name = "service_1")
    public DataSource service1() throws Exception {
        DruidDataSource datasource = new DruidDataSource();
        Properties properties = new Properties();
        try {
            properties.load(this.getClass().getClassLoader().getResourceAsStream("service1.properties"));
            datasource.setConnectProperties(properties);
            log.info("数据库 :" + datasource.getUrl() + " 初始化成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return datasource;
    }
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(MultipleDataSource source) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(source);
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:/mapper/**/*.xml");
        sqlSessionFactoryBean.setMapperLocations(resources);
       // sessionFactory.setTypeAliasesPackage(DataSourceConfig.ALIASES_PACKAGE);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.lin.sys.entity");
        return sqlSessionFactoryBean;
    }

    @Bean
    public DataSourceTransactionManager getTxManager(MultipleDataSource source) throws Exception {
        return new DataSourceTransactionManager(source);
    }

    @Bean
    public MultipleDataSource multipleDataSource(@Qualifier("service_0") DataSource service0, @Qualifier("service_1") DataSource service1) {
        MultipleDataSource multipleDataSource = new MultipleDataSource();
        multipleDataSource.setDefaultTargetDataSource(service0);
        HashMap<Object, Object> targetDataSources = new HashMap<>(2);
        targetDataSources.put("service_0", service0);
        targetDataSources.put("service_1", service1);
        multipleDataSource.setTargetDataSources(targetDataSources);
        return multipleDataSource;
    }

    @Bean
    public PageHelper pageHelper() {
        //分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}
